home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Storable.pm < prev    next >
Encoding:
Text File  |  1999-12-28  |  9.4 KB  |  288 lines

  1. ;# $Id: Storable.pm,v 0.5 1997/06/10 16:38:37 ram Exp $
  2. ;#
  3. ;#  Copyright (c) 1995-1997, Raphael Manfredi
  4. ;#  
  5. ;#  You may redistribute only under the terms of the Artistic License,
  6. ;#  as specified in the README file that comes with the distribution.
  7. ;#
  8. ;# $Log: Storable.pm,v $
  9. ;# Revision 0.5  1997/06/10  16:38:37  ram
  10. ;# Baseline for fifth alpha release.
  11. ;#
  12.  
  13. require DynaLoader;
  14. require Exporter;
  15. package Storable; @ISA = qw(Exporter DynaLoader);
  16.  
  17. @EXPORT = qw(store retrieve);
  18. @EXPORT_OK = qw(
  19.     nstore store_fd nstore_fd retrieve_fd
  20.     freeze nfreeze thaw
  21.     dclone
  22. );
  23.  
  24. use AutoLoader;
  25. use Carp;
  26. use vars qw($forgive_me $VERSION);
  27.  
  28. $VERSION = '0.5';
  29. *AUTOLOAD = \&AutoLoader::AUTOLOAD;        # Grrr...
  30.  
  31. bootstrap Storable;
  32. 1;
  33. __END__
  34.  
  35. sub store {
  36.     return _store(0, @_);
  37. }
  38.  
  39. sub nstore {
  40.     return _store(1, @_);
  41. }
  42.  
  43. sub _store {
  44.     my $netorder = shift;
  45.     my $self = shift;
  46.     my ($file) = @_;
  47.     croak "Not a reference" unless ref($self);
  48.     croak "Too many arguments" unless @_ == 1;    # Watch out for @foo in arglist
  49.     local *FILE;
  50.     open(FILE, ">$file") || croak "Can't create $file: $!";
  51.     binmode FILE;
  52.     my $ret;
  53.     eval { $ret = $netorder ? net_pstore(FILE, $self) : pstore(FILE, $self) };
  54.     close(FILE) or $ret = undef;
  55.     unlink($file) or warn "Can't unlink $file: $!\n" if $@ || !defined $ret;
  56.     croak $@ if $@ =~ s/\.?\n$/,/;
  57.     return $ret ? $ret : undef;
  58. }
  59.  
  60. sub store_fd {
  61.     return _store_fd(0, @_);
  62. }
  63.  
  64. sub nstore_fd {
  65.     my ($self, $file) = @_;
  66.     return _store_fd(1, @_);
  67. }
  68.  
  69. sub _store_fd {
  70.     my $netorder = shift;
  71.     my $self = shift;
  72.     my ($file) = @_;
  73.     croak "Not a reference" unless ref($self);
  74.     croak "Too many arguments" unless @_ == 1;    # Watch out for @foo in arglist
  75.     my $fd = fileno($file);
  76.     croak "Not a valid file descriptor" unless defined $fd;
  77.     my $ret;
  78.     eval { $ret = $netorder ? net_pstore($file, $self) : pstore($file, $self) };
  79.     croak $@ if $@ =~ s/\.?\n$/,/;
  80.     return $ret ? $ret : undef;
  81. }
  82.  
  83. sub freeze {
  84.     _freeze(0, @_);
  85. }
  86.  
  87. sub nfreeze {
  88.     _freeze(1, @_);
  89. }
  90.  
  91. sub _freeze {
  92.     my $netorder = shift;
  93.     my $self = shift;
  94.     croak "Not a reference" unless ref($self);
  95.     croak "Too many arguments" unless @_ == 0;    # Watch out for @foo in arglist
  96.     my $ret;
  97.     eval { $ret = $netorder ? net_mstore($self) : mstore($self) };
  98.     croak $@ if $@ =~ s/\.?\n$/,/;
  99.     return $ret ? $ret : undef;
  100. }
  101. sub retrieve {
  102.     my ($file) = @_;
  103.     local *FILE;
  104.     open(FILE, "$file") || croak "Can't open $file: $!";
  105.     binmode FILE;
  106.     my $self;
  107.     eval { $self = pretrieve(FILE) };        # Call C routine
  108.     close(FILE);
  109.     croak $@ if $@ =~ s/\.?\n$/,/;
  110.     return $self;
  111. }
  112.  
  113. sub retrieve_fd {
  114.     my ($file) = @_;
  115.     my $fd = fileno($file);
  116.     croak "Not a valid file descriptor" unless defined $fd;
  117.     my $self;
  118.     eval { $self = pretrieve($file) };        # Call C routine
  119.     croak $@ if $@ =~ s/\.?\n$/,/;
  120.     return $self;
  121. }
  122.  
  123. sub thaw {
  124.     my ($frozen) = @_;
  125.     my $self;
  126.     eval { $self = mretrieve($frozen) };    # Call C routine
  127.     croak $@ if $@ =~ s/\.?\n$/,/;
  128.     return $self;
  129. }
  130.  
  131. =head1 NAME
  132.  
  133. Storable - persistency for perl data structures
  134.  
  135. =head1 SYNOPSIS
  136.  
  137.     use Storable;
  138.     store \%table, 'file';
  139.     $hashref = retrieve('file');
  140.  
  141. =head1 DESCRIPTION
  142.  
  143. The Storable package brings you persistency for your perl data structures
  144. containing SCALAR, ARRAY, HASH or REF objects, i.e. anything that can be
  145. convenientely stored to disk and retrieved at a later time.
  146.  
  147. It can be used in the regular procedural way by calling C<store> with
  148. a reference to the object to store, and providing a file name. The routine
  149. returns C<undef> for I/O problems or other internal error, a true value
  150. otherwise. Serious errors are propagated as a C<die> exception.
  151.  
  152. To retrieve data stored to disk, you use C<retrieve> with a file name,
  153. and the objects stored into that file are recreated into memory for you,
  154. and a I<reference> to the root object is returned. In case an I/O error
  155. occurred while reading, C<undef> is returned instead. Other serious
  156. errors are propagated via C<die>.
  157.  
  158. Since storage is performed recursively, you might want to stuff references
  159. to objects that share a lot of common data into a single array or hash
  160. table, and then store that object. That way, when you retrieve back the
  161. whole thing, the objects will continue to share what they originally shared.
  162.  
  163. At the cost of a slight header overhead, you may store to an already
  164. opened file descriptor using the C<store_fd> routine, and retrieve
  165. from a file via C<retrieve_fd>. Those names aren't imported by default,
  166. so you will have to do that explicitely if you need those routines.
  167. The file descriptor name you supply must be fully qualified.
  168.  
  169. You can also store data in network order to allow easy sharing across
  170. multiple platforms, or when storing on a socket known to be remotely
  171. connected. The routines to call have an initial C<n> prefix for I<network>,
  172. as in C<nstore> and C<nstore_fd>. At retrieval time, your data will be
  173. correctly restored so you don't have to know whether you're restoring
  174. from native or network ordered data.
  175.  
  176. When using C<retrieve_fd>, objects are retrieved in sequence, one
  177. object (i.e. one recursive tree) per associated C<store_fd>.
  178.  
  179. If you're more from the object-oriented camp, you can inherit from
  180. Storable and directly store your objects by invoking C<store> as
  181. a method. The fact that the root of the to-be-stored tree is a
  182. blessed reference (i.e. an object) is special-cased so that the
  183. retrieve does not provide a reference to that object but rather the
  184. blessed object reference itself. (Otherwise, you'd get a reference
  185. to that blessed object).
  186.  
  187. =head1 MEMORY STORE
  188.  
  189. The Storable engine can also store data into a Perl scalar instead, to
  190. later retrieve them. This is mainly used to freeze a complex structure in
  191. some safe compact memory place (where it can possibly be sent to another
  192. process via some IPC, since freezing the structure also serializes it in
  193. effect). Later on, and maybe somewhere else, you can thaw the Perl scalar
  194. out and recreate the original complex structure in memory.
  195.  
  196. Surprisingly, the routines to be called are named C<freeze> and C<thaw>.
  197. If you wish to send out the frozen scalar to another machine, use
  198. C<nfreeze> instead to get a portable image.
  199.  
  200. Note that freezing an object structure and immediately thawing it
  201. actually achieves a deep cloning of that structure. Storable provides
  202. you with a C<dclone> interface which does not create that intermediary
  203. scalar but instead freezes the structure in some internal memory space
  204. and then immediatly thaws it out.
  205.  
  206. =head1 SPEED
  207.  
  208. The heart of Storable is written in C for decent speed. Extra low-level
  209. optimization have been made when manipulating perl internals, to
  210. sacrifice encapsulation for the benefit of a greater speed.
  211.  
  212. Storage is usually faster than retrieval since the latter has to
  213. allocate the objects from memory and perform the relevant I/Os, whilst
  214. the former mainly performs I/Os.
  215.  
  216. On my HP 9000/712 machine running HPUX 9.03 and with perl 5.004, I can
  217. store 0.8 Mbyte/s and I can retrieve at 0.72 Mbytes/s, approximatively
  218. (CPU + system time).
  219. This was measured with Benchmark and the I<Magic: The Gathering>
  220. database from Tom Christiansen (1.9 Mbytes).
  221.  
  222. =head1 EXAMPLES
  223.  
  224. Here are some code samples showing a possible usage of Storable:
  225.  
  226.     use Storable qw(store retrieve freeze thaw dclone);
  227.  
  228.     %color = ('Blue' => 0.1, 'Red' => 0.8, 'Black' => 0, 'White' => 1);
  229.  
  230.     store(\%color, '/tmp/colors') or die "Can't store %a in /tmp/colors!\n";
  231.  
  232.     $colref = retrieve('/tmp/colors');
  233.     die "Unable to retrieve from /tmp/colors!\n" unless defined $colref;
  234.     printf "Blue is still %lf\n", $colref->{'Blue'};
  235.  
  236.     $colref2 = dclone(\%color);
  237.  
  238.     $str = freeze(\%color);
  239.     printf "Serialization of %%color is %d bytes long.\n", length($str);
  240.     $colref3 = thaw($str);
  241.  
  242. which prints (on my machine):
  243.  
  244.     Blue is still 0.100000
  245.     Serialization of %color is 102 bytes long.
  246.  
  247. =head1 WARNING
  248.  
  249. If you're using references as keys within your hash tables, you're bound
  250. to disapointment when retrieving your data. Indeed, Perl stringifies
  251. references used as hash table keys. If you later wish to access the
  252. items via another reference stringification (i.e. using the same
  253. reference that was used for the key originally to record the value into
  254. the hash table), it will work because both references stringify to the
  255. same string.
  256.  
  257. It won't work across a C<store> and C<retrieve> operations however, because
  258. the addresses in the retrieved objects, which are part of the stringified
  259. references, will probably differ from the original addresses. The
  260. topology of your structure is preserved, but not hidden semantics
  261. like those.
  262.  
  263. On platforms where it matters, be sure to call C<binmode()> on the
  264. descriptors that you pass to Storable functions.
  265.  
  266. =head1 BUGS
  267.  
  268. You can't store GLOB, CODE, FORMLINE, etc... If you can define
  269. semantics for those operations, feel free to enhance Storable so that
  270. it can deal with those.
  271.  
  272. The store functions will C<croak> if they run into such references
  273. unless you set C<$Storable::forgive_me> to some C<TRUE> value. In this
  274. case, the fatal message is turned in a warning and some
  275. meaningless string is stored instead.
  276.  
  277. Due to the aforementionned optimizations, Storable is at the mercy
  278. of perl's internal redesign or structure changes. If that bothers
  279. you, you can try convincing Larry that what is used in Storable
  280. should be documented and consistently kept in future revisions.
  281. As I said, you may try.
  282.  
  283. =head1 AUTHOR
  284.  
  285. Raphael Manfredi F<E<lt>Raphael_Manfredi@grenoble.hp.comE<gt>>
  286.  
  287. =cut
  288.